using System;
using System.Collections.Generic;
using System.Linq;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Media;
using Jemgine.Hex;

namespace HexDemo
{
    internal class HexVertexBuffers
    {
        internal VertexBuffer HexVertexBuffer;
        internal IndexBuffer HexIndexBuffer;
    }

    internal class DrawHex
    {
        internal static void PrepareVertexBuffers(HexVertexBuffers Buffers, 
            CoordinateSystem CoordinateSystem,
            GraphicsDevice GraphicsDevice)
        {
            Matrix PositivePointRotation = Matrix.CreateRotationZ(MathHelper.ToRadians(60));

            {
                List<VertexPositionColorTexture> Verticies = new List<VertexPositionColorTexture>();

                Verticies.Add(new VertexPositionColorTexture(Vector3.Zero, Color.White, new Vector2(0.5f, 0.5f)));

                Vector3 Point;
                if (CoordinateSystem.GetOrientation() == Orientation.Horizontal)
                    Point = new Vector3(-(float)((CoordinateSystem as HorizontalCoordinateSystem).Radius), 0, 0);
                else
                    Point = new Vector3(0, -(float)((CoordinateSystem as VerticalCoordinateSystem).Radius), 0);

                
                for (int i = 0; i < 6; ++i)
                {
                    Vector2 TextureCoord = new Vector2(Point.X, Point.Y);
                    TextureCoord /= ((float)CoordinateSystem.GetRadius() * 2);
                    TextureCoord += new Vector2(0.5f, 0.5f);
                    Verticies.Add(new VertexPositionColorTexture(Point, Color.White, TextureCoord));
                    Point = Vector3.Transform(Point, PositivePointRotation);
                }

               

                List<Int16> Indexes = new List<Int16>();
                for (Int16 i = 0; i < 5; ++i)
                    Indexes.AddRange(new Int16[3] { 0, (Int16)(i + 2), (Int16)(i + 1) });
                Indexes.AddRange(new Int16[3] { 0, 1, 6 });

               

                Buffers.HexVertexBuffer = new VertexBuffer(GraphicsDevice, typeof(VertexPositionColorTexture),
                    Verticies.Count, BufferUsage.WriteOnly);
                Buffers.HexVertexBuffer.SetData(Verticies.ToArray());

                Buffers.HexIndexBuffer = new IndexBuffer(GraphicsDevice, typeof(Int16), Indexes.Count, BufferUsage.WriteOnly);
                Buffers.HexIndexBuffer.SetData(Indexes.ToArray());
            }
        }

        internal static void DrawTexturedHex(
            HexVertexBuffers Buffers, 
            Texture2D Texture, 
            ImmediateMode Renderer, 
            Vector2 Position, 
            GraphicsDevice GraphicsDevice)
        {
            Renderer.BindTexture(Texture);
            Renderer.PushMatrix(Matrix.CreateTranslation(Position.X, Position.Y, 0));
            Renderer.Prepare();
            GraphicsDevice.SetVertexBuffer(Buffers.HexVertexBuffer);
            GraphicsDevice.Indices = Buffers.HexIndexBuffer;
            GraphicsDevice.DrawIndexedPrimitives(PrimitiveType.TriangleList, 0, 0, Buffers.HexIndexBuffer.IndexCount,
                0, Buffers.HexIndexBuffer.IndexCount / 3);
            Renderer.PopMatrix();
        }

    }
}
